//
//  NSDate+ZXCategory.m
//  MobileCaiLocal
//
//  Created by simon on 2021/3/19.
//  Copyright © 2021 com.Chs. All rights reserved.
//

#import "NSDate+ZXCategory.h"
#import <mach/mach_time.h>

#pragma mark - secondsTime
//a day/month/year has many secondes;
#ifndef SECONDS_PER_HOUR
#define SECONDS_PER_HOUR (60*60)
#endif
#ifndef SECONDS_PER_DAY
#define SECONDS_PER_DAY (24*60*60)
#endif
#ifndef SECONDS_PER_MONTH
#define SECONDS_PER_MONTH (30*24*60*60)
#endif
#ifndef SECONDS_PER_YEAR
#define SECONDS_PER_YEAR (365*24*60*60)
#endif

@implementation NSDate (ZXCategory)




+ (NSTimeInterval)zx_timeIntervalSinceAnotherDateString:(NSString *)dateString
                                             dateFormat:(NSString *)format
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:format ?format : @"yyyy-MM-dd HH:mm:ss"];
    //string型转换为NSDate型
    NSDate *anotherDate = [formatter dateFromString:dateString];
    NSTimeInterval ti = [anotherDate timeIntervalSinceNow];
    return ti;
}

+ (nullable NSString *)zx_stringValueWithTimeDifference:(NSTimeInterval)time
                                            showSeconds:(BOOL)flag
                                                 format:(BOOL)formatting
{
    NSMutableString *timeString = [NSMutableString string];
    if (time <= 0)
    {
        if (formatting) {
            return nil;
        }
        [timeString appendFormat:@"0天0小时0分"];
        if (flag)
        {
            [timeString appendFormat:@"0秒"];
        }
        return timeString;
    }
    
    int newTime = ABS(time);
    int mon   = newTime/(3600*24*30);
    int days  = (newTime%(3600*24*30))/SECONDS_PER_DAY;
    int hours = ((newTime%(3600*24*30))%SECONDS_PER_DAY)/SECONDS_PER_HOUR;
    int min     = (((newTime%(3600*24*30))%SECONDS_PER_DAY)%SECONDS_PER_HOUR)/60;
    int second = (((newTime%(3600*24*30))%SECONDS_PER_DAY)%SECONDS_PER_HOUR)%60;
    if (mon>0)
    {
        [timeString appendFormat:@"%i月",mon];
    }
    if (formatting) {
        if (days>0)
        {
            [timeString appendFormat:@"%i天",days];
        }
        if (hours>0)
        {
            [timeString appendFormat:@"%i小时",hours];
        }
        if (min>0)
        {
            [timeString appendFormat:@"%i分",min];
        }
        if (flag)
        {
            [timeString appendFormat:@"%i秒",second];
        }
    }
    else
    {
        [timeString appendFormat:@"%i天",days];
        [timeString appendFormat:@"%i小时",hours];
        [timeString appendFormat:@"%i分",min];
        if (flag)
        {
            [timeString appendFormat:@"%i秒",second];
        }
    }
 
    return timeString;
}



///1秒=1000毫秒 1毫秒=1000微秒 1微秒=1000纳秒
+ (double)currentScopeAbsoluteTimeBlock:(void(^)(void))block
{
    mach_timebase_info_data_t info;
    if (mach_timebase_info(&info) != KERN_SUCCESS) {
        return -1.0;
    }
    ///返回一个基于系统启动后的时钟tick(嘀嗒)数。并且，它包含系统时钟所包含的所有时间区域。纳秒级精度。
    uint64_t start = mach_absolute_time();
    if (block) {
        block();
    }
    uint64_t end = mach_absolute_time();
    uint64_t elapsed = end - start;
    uint64_t nanos = elapsed * info.numer / info.denom;
    double test = (double)nanos /NSEC_PER_SEC;
    return test;
}


+ (CFTimeInterval)currentTickMediaTime
{
    mach_timebase_info_data_t info;
    if (mach_timebase_info(&info) != KERN_SUCCESS) {
        return -1.0;
    }
    /// 返回当前的CoreAnimation绝对时间。这是调用mach_absolute_time()并将单位转换为秒的结果
    CFTimeInterval startTime = CACurrentMediaTime();
    return startTime;
}

@end
